package com.chenwc.util.fileutil.jsonfile;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.chenwc.enumeration.TranslateSoftware;
import com.chenwc.util.Util;

import java.util.HashMap;

/**
 * @author chenwc
 * @package com.chenwc.util.fileutil.jsonfile
 * @project wso2-sinicization
 * @date 2024/5/25
 */
public class JsonFileUtil {

    /**
     * 递归翻译json对象
     *
     * @param json              json对象
     * @param terminology       自定义翻译词典
     * @param translateSoftware 翻译软件
     * @return 转换后的json对象
     */
    public static JSONObject recursionJsonObject(JSONObject json, HashMap<String, String> terminology, TranslateSoftware translateSoftware) throws Exception {
        JSONObject rst = new JSONObject();
        for (String key : json.keySet()) {
            Object object = json.get(key);
            // JSON对象
            if (object instanceof JSONObject) {
                rst.put(key, recursionJsonObject((JSONObject) object, terminology, translateSoftware));
            }
            // JSON数组
            else if (object instanceof JSONArray) {
                rst.put(key, recursionJsonArray(json.getJSONArray(key), terminology, translateSoftware));
            }
            // 值
            else {
                //字符串
                if (object instanceof String) {
                    String str = (String) object;
                    String cntext = null;
                    if (!"type".equals(key)) {
                        if (terminology.containsKey(str.toLowerCase())) {
                            cntext = terminology.get(str.toLowerCase());
                        } else {
                            cntext = Util.getTranslateValue(str, translateSoftware);
                        }
                    }
                    if (Util.isEmpty(cntext)) {
                        rst.put(key, str);
                    } else {
                        rst.put(key, Util.valueReplace(cntext));
                    }
                } else {
                    rst.put(key, object);
                }
            }
        }
        return rst;
    }

    /**
     * 递归json数组
     *
     * @param jsonArray         json数组
     * @param terminology       自定义翻译词典
     * @param translateSoftware 翻译软件
     * @return 转换后的json数组
     */
    public static JSONArray recursionJsonArray(JSONArray jsonArray, HashMap<String, String> terminology, TranslateSoftware translateSoftware) throws Exception {
        JSONArray rst = new JSONArray();
        for (Object object : jsonArray) {
            // JSON对象
            if (object instanceof JSONObject) {
                rst.add(recursionJsonObject((JSONObject) object, terminology, translateSoftware));
            }
            // JSON数组
            else if (object instanceof JSONArray) {
                rst.add(recursionJsonArray((JSONArray) object, terminology, translateSoftware));
            }
            // 值
            else {
                //字符串
                if (object instanceof String) {
                    String str = (String) object;
                    String cntext;
                    if (terminology.containsKey(str.toLowerCase())) {
                        cntext = terminology.get(str.toLowerCase());
                    } else {
                        cntext = Util.getTranslateValue(str, translateSoftware);
                    }
                    if (Util.isEmpty(cntext)) {
                        rst.add(str);
                    } else {
                        rst.add(Util.valueReplace(cntext));
                    }
                } else {
                    rst.add(object);
                }
            }
        }
        return rst;
    }
}
